<template>
	<view class="col363636 f26r pl20r pr20r pb20r">
		<view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				保洁账号：
			</view>
			<view class="flex1 pl20r">
				<input type="text" placeholder="请输入保洁账号"
					class="f26r h60r" v-model="newObj.username"
				>
			</view>
		</view>
		<view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				保洁密码：
			</view>
			<view class="flex1 pl20r">
				<input type="password" placeholder="请输入保洁密码"
					class="f26r h60r" v-model="newObj.password"
				>
			</view>
		</view>
		<view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				确认密码：
			</view>
			<view class="flex1 pl20r">
				<input type="password" placeholder="请输入确认密码"
					class="f26r h60r" v-model="affirmPsd"
				>
			</view>
		</view>
		<view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				保洁名：
			</view>
			<view class="flex1 pl20r">
				<input type="text" placeholder="请输入保洁名"
					class="f26r h60r" v-model="newObj.name"
				>
			</view>
		</view>
		<view class="disFlex aliItCenter h100r borB1SE8e8e8">
			<view class="w160r teAliRight borR1SE8e8e8">
				启用/禁用：
			</view>
			<view class="flex1 pl20r">
				<radio-group @change="radioChange1">
					<label v-for="item in radioAry1" :key="item.value"
						class="wPer50 disInBlo teAliCenter"
					>
						<radio :value="item.value"
							:checked="item.value === newObj.type"
							color="#64aeb4" style="transform: scale(0.8)"
						 />
						{{item.radioText}}
					</label>
				</radio-group>
			</view>
		</view>
		<button class="colFff lh70r mt40r f26r backCol64aeb4"
			@click="cliSubBtn"
		>
			提交
		</button>
	</view>
</template>
<script>
import { inquiryToLogin } from '@/utils/utils.js'
import { addTeaClean, saveTeaClean } from '@/utils/api.js'
export default {
	data() {
		return {
			optionType: '',
			optionItem: {},
			radioAry1: [
				{value: '1', radioText: '启用'},
				{value: '2', radioText: '禁用'}
			],
			newObj: {
				token: '',
				username: '',
				password: '',
				name: '',
				type: '1'
			},
			affirmPsd: ''
		}
	},
	onLoad(options) {
		this.optionType = options.type;
		if(options.type == 1) {
			uni.setNavigationBarTitle({ title: '新增保洁' });
		}else if(options.type == 2) {
			uni.setNavigationBarTitle({ title: '修改保洁' });
			this.optionItem = JSON.parse(options.item);
			console.log(this.optionItem)
			this.newObj.username = this.optionItem.username;
			this.newObj.name = this.optionItem.name;
			this.newObj.type = this.optionItem.type;
			this.newObj.id = this.optionItem.id;
		}
	},
	onShow() {
		inquiryToLogin().then(res => {
			this.newObj.token = res;
		});
	},
	methods: {
		radioChange1(evt) {
			this.newObj.type = evt.detail.value;
		},
		cliSubBtn() {
			if(!this.newObj.username) return uni.showToast({ title: '请输入保洁账号', icon: 'none' })
			if(!this.newObj.password) return uni.showToast({ title: '请输入保洁密码', icon: 'none' })
			if(!this.affirmPsd) return uni.showToast({ title: '请输入确认密码', icon: 'none' })
			if(this.newObj.password !== this.affirmPsd) {
				return uni.showToast({
					title: '两次密码不一致', icon: 'none', duration: 3000
				})
			}
			if(!this.newObj.name) return uni.showToast({ title: '请输入保洁名', icon: 'none' })
			if(this.optionType == 1) {
				addTeaClean(this.newObj).then(res1 => {
					uni.showModal({
						title: '提示', content: '新增保洁成功', confirmColor: '#99c043',
						showCancel: false, confirmText: '返回保洁列表',
						success:res2 => {
							if (res2.confirm) {
								uni.navigateBack({ delta: 1 });
							}
						}
					})
				})
			}else if(this.optionType == 2) {
				saveTeaClean(this.newObj).then(res1 => {
					console.log(11, res1)
					uni.showModal({
						title: '提示', content: '修改保洁成功', confirmColor: '#99c043',
						showCancel: false, confirmText: '返回保洁列表',
						success:res2 => {
							if (res2.confirm) {
								uni.navigateBack({ delta: 1 });
							}
						}
					})
				})
			}
		}
	}
}
</script>
<style>
</style>